package controllers.manage.product;

import controllers.BaseController;
import controllers.auth.Secure;
import enums.ProductStatus;
import helper.GlobalConfig;
import models.constants.DeletedStatus;
import models.merchant.Merchant;
import org.apache.commons.lang.StringUtils;
import play.modules.paginate.JPAExtPaginator;
import play.mvc.Controller;
import play.mvc.With;
import product.Area;
import product.Product;
import product.type.ProductType;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/22.
 */
@With(BaseController.class)
public class ProductAreaCtrl extends Controller{
    public static void index(Integer pageNumber, String name){
        Merchant merchant = Secure.getLoginUser().merchant;
        pageNumber = pageNumber == null ? 1 : pageNumber;
        Map<String , Object> searchMap = new HashMap<>();
        if(StringUtils.isNotBlank(name)){
            searchMap.put("name", "%"+name+"%");
        }
        JPAExtPaginator<Area> resultPage = Area.findByCondition(searchMap, "createAt desc", pageNumber, GlobalConfig.PAGE_SIZE);
        render(resultPage, pageNumber,name);
    }
    public static void add(Area area){
        if(area.id!=null && !area.id.equals("")){
            area.modifyAt=new Date();
            area.save();
            flash.put("msg" , "区域修改成功");
        }else {
            area.createAt=new Date();
            area.deleted= DeletedStatus.UN_DELETED;
            area.save();
            flash.put("msg" , "区域新增成功");
        }
        index(1,null);
    }
    public static void del(Long id,Integer pageNumber){
        pageNumber = pageNumber == null ? 1 : pageNumber;
        Area area=Area.fingById(id);
        area.deleted=DeletedStatus.DELETED;
        area.save();
        flash.put("msg" , "区域删除成功");
        index(1,null);
    }
}
